package encome.work.controller;

import encome.work.dao.FastDFSFile;
import encome.work.dao.ResponseResult;
import encome.work.utils.FastDFSUtil;
import encome.work.utils.FileException;
import encome.work.utils.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/file")
@CrossOrigin
@Slf4j
public class FileController {
    @Resource
    private HttpServletResponse response;
    /**
     * 文件上传
     */
    @PostMapping
    public ResponseResult<String> upload(@RequestParam("file")MultipartFile file){
        if(file.isEmpty()){
            throw new FileException("文件不存在");
        }
        String url = null;
        try{
            FastDFSFile fastDFSFile = new FastDFSFile(
                    file.getOriginalFilename(), //文件名  xxx.jpg
                    file.getBytes(),  //文件字节数组
                    StringUtils.getFilenameExtension(file.getOriginalFilename()) //文件扩展名
            );
            //调用工具类FastDFSUtil
            String[] uploads = FastDFSUtil.upload(fastDFSFile);

            //拼接访问地址
            url = FastDFSUtil.getTrackerInfo() + "/" + uploads[0] + "/" + uploads[1];
        }catch (Exception e){
            throw new FileException("文件上传失败");
        }
        return new ResponseResult<>(StatusCode.OK, "上传成功", url);
    }

    @GetMapping
    public ResponseResult<String> download(@RequestParam("groupName") String groupName,
                                           @RequestParam("remoteFileName") String remoteFileName) {
        try {
            String[] split = remoteFileName.split("/");
            String fileName = split[split.length-1];

            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        }catch (Exception e){
            throw new FileException("解析文件信息失败");
        }

        try{
            InputStream inputStream = FastDFSUtil.downloadFile(groupName,remoteFileName);

            ServletOutputStream outputStream = response.getOutputStream();

            byte[] buffer = new byte[1024];
            while(inputStream.read(buffer) != -1){
                outputStream.write(buffer);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        }catch (Exception e){
            throw new FileException("文件下载失败");
        }

        return new ResponseResult<>(StatusCode.OK, "下载成功");
    }

    @DeleteMapping
    public ResponseResult<String> deleteFile(@RequestParam("groupName") String groupName,
                                           @RequestParam("remoteFileName") String remoteFileName) {

        try {
            FastDFSUtil.deleteFile(groupName, remoteFileName);
        }catch (Exception e){
            throw new FileException("文件删除失败");
        }

        return new ResponseResult<>(StatusCode.OK, "删除成功");
    }
}
